﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraCharts;
using DevExpress.XtraEditors;
using Lamex.Base;
using Lamex.Library.CommonClass;

namespace Lamex.Library.UserCotrolUI
{
    public partial class FrmOEEControlUI : DevExpress.XtraEditors.XtraUserControl
    {
        public FrmOEEControlUI()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 设置站别名称11
        /// </summary>
        /// <param name="ProcessName"></param>

        public void ShowProcessList(string ProcessCode, int iStandTime,string UserName)
        {
            try
            {
                if (string.IsNullOrEmpty(ProcessCode))
                {
                    XtraMessageBox.Show("没有输入工序号");
                    return;
                }

                DataTable dtCheck = BaseBuildSql.GetDataTableBySql(@"
                                                                select top 10* from [dbo].[HR_Scan_Record] 
where  ISNULL(TIME_LENGTH,0) =0 and  CONVERT(nvarchar(10),SCAN_TIME,120)=CONVERT(nvarchar(10),GETDATE(),120) 
and STATION_NAME='"+UserName+@"' and (NOID!=0 or isnull(NOID,'')!='' )and (   NOID!=888 or NOID!=999) ");
                if (dtCheck.Rows.Count == 0) return;

                DataTable dtProcess = BaseBuildSql.GetDataTableBySql(@"select CONVERT(nvarchar(6),BARCODE) BARCODE   , CEILING (sum (ISNULL(TIME_LENGTH,0.0))/60.0) iSum  from [dbo].[HR_Scan_Record] 
where ISNULL(TIME_LENGTH,0) !=0   and  CONVERT(nvarchar(10),SCAN_TIME,120)=CONVERT(nvarchar(10),GETDATE(),120)
and STATION_NAME='" + UserName + @"'
group by CONVERT(nvarchar(6),BARCODE) 
union all
select 'MES', isnull(n.Timms,0)-isnull(m.iSum,0) as  iSum from (select CEILING(sum (ISNULL(TIME_LENGTH,0.0))/60.0) iSum ,'MES' as MES from [dbo].[HR_Scan_Record] 
where ISNULL(TIME_LENGTH,0) !=0   and  CONVERT(nvarchar(10),SCAN_TIME,120)=CONVERT(nvarchar(10),GETDATE(),120)
and STATION_NAME='" + UserName + @"'
)  m 
left join ( select DATEDIFF(MINUTE,CONVERT(time(7),c.T1_START ),CONVERT(time(7),GETDATE()))-10  as Timms ,'MES' as MES  from (
select a.T1_START from  [dbo].[HR_Work_ShiftRec]  a 
join [HR_Station_Info] b on b.DEPARTMENT=a.DEPARTMENT
where b.STATION_NAME='" + UserName + @"' and a.EFFECTIVE_DATE<  CONVERT(nvarchar(10),GETDATE(),120) and CONVERT(nvarchar(10),GETDATE(),120)<CONVERT(nvarchar(10), a. EXPIRY_DATE,120)
group by a.DEPARTMENT,a.T1_START) c ) n on n.MES = m.MES
                                                                ");

                string ProcessName = "OEE";
                chartOEEUI.Series.Clear();

                Series seriesAuto = new Series("", ViewType.Pie);
                dtProcess.Columns.Add("Title");

                string dt3Sum = dtProcess.Compute("sum(iSum)", "true").ToString();

                if (string.IsNullOrEmpty(dt3Sum)) return;

                if (int.Parse(dt3Sum) == 0)
                {
                    string StatusName = "合格率";
                    SeriesPoint p = new SeriesPoint();

                    double[] d = new double[] { double.Parse((1.0 * 100).ToString()) };
                    //p.Values
                    p.Values = d;
                    p.Color = ColorTypeHelper.GetColorType("MES", ref StatusName);
                    string percent = StatusName + "(" + d+ "%" + ")";
                    p.Argument = percent;
                    seriesAuto.Points.Add(p);
                }
                else
                {
                    foreach (DataRow dr in dtProcess.Rows)
                    {
                        //dr["Title"] = percent;
                        string StatusName = "";
                        SeriesPoint p = new SeriesPoint();

                        double[] d = new double[] { double.Parse(Math.Round((decimal.Parse(dr["iSum"].ToString()) / decimal.Parse(dt3Sum)) * 100, 2).ToString()) };
                        //p.Values
                        p.Values = d;

                        if (dr["BARCODE"].ToString() == "MES_QC")
                            p.Color = ColorTypeHelper.GetColorType("MES_QC1", ref StatusName);
                        else
                            p.Color = ColorTypeHelper.GetColorType(dr["BARCODE"].ToString(), ref StatusName);

                        string percent = StatusName + "(" + Math.Round((decimal.Parse(dr["iSum"].ToString()) / decimal.Parse(dt3Sum)) * 100, 2).ToString() + "%" + ")";
                        p.Argument = percent;
                        seriesAuto.Points.Add(p);
                    }

                }
                //seriesAuto.DataSource = dtProcess;
                //seriesAuto.ArgumentScaleType = ScaleType.Qualitative;
                ////名称
                //seriesAuto.ArgumentDataMember = "BARCODE";
                ////名称
                //seriesAuto.ArgumentDataMember = "Title";
                //seriesAuto.ValueScaleType = ScaleType.Numerical;

                ////取值字段 
                //seriesAuto.ValueDataMembers.AddRange(new string[] { "iSum" });
                //(seriesAuto.Label as PieSeriesLabel).Position = PieSeriesLabelPosition.Outside;
                //seriesAuto.Label.TextPattern = "as";
                //显示标项目名称和百分比数据
                seriesAuto.Label.Font = new System.Drawing.Font("Tahoma", 6F);
                seriesAuto.PointOptions.PointView = PointView.Argument;
                seriesAuto.PointOptions.ValueNumericOptions.Format = NumericFormat.Percent;




                //seriesAuto.Visible = false;
                //(seriesAuto.Label as PieSeriesLabel).Position = PieSeriesLabelPosition.Outside;
                //显示标项目名称和百分比数据
                //seriesAuto.PointOptions.PointView = PointView.ArgumentAndValues;
                seriesAuto.PointOptions.ValueNumericOptions.Format = NumericFormat.General;


                //_pieView.RuntimeExploding = false;
                //seriesAuto.LabelsVisibility = DevExpress.Utils.DefaultBoolean.True;//每个点都显示数据
                //ChartTitle autoTitle = new ChartTitle();
                //autoTitle.Text = "Hour";
                //chartHour.Titles.Clear();
                //chartHour.Titles.Add(autoTitle);

                this.chartOEEUI.Series.Add(seriesAuto);
                chartOEEUI.Legend.Visibility = DevExpress.Utils.DefaultBoolean.False;
                Legend Legends = (Legend)chartOEEUI.Legend;

                Legends.Visibility = DevExpress.Utils.DefaultBoolean.False;
                Legends.AlignmentVertical = LegendAlignmentVertical.Center;
                Legends.AlignmentHorizontal = LegendAlignmentHorizontal.Center;
                Legends.TextVisible = false;


                PieSeriesView _pieView = (PieSeriesView)seriesAuto.View;
                _pieView.ExplodeMode = PieExplodeMode.All;
                _pieView.ExplodedDistancePercentage = 50;


                //seriesAuto.Legend=(Legend)chartOEEUI.Diagram;
                //diagramhour = (XYDiagram)chartOEEUI.Diagram;


                //seriesAuto.Legend.Visibility = DevExpress.Utils.DefaultBoolean.False;
                //XYDiagram diagramhour = (XYDiagram)chartOEEUI.Diagram;
                //diagramhour.AxisY.ConstantLines.Clear();
                ////diagramhour.EnableAxisXScrolling = true;
                ////diagramhour.EnableAxisXZooming = true;
                ////diagramhour.EnableAxisYScrolling = true;
                ////diagramhour.EnableAxisYZooming = true;
                //diagramhour.AxisY.VisualRange.MinValue = iStandTime * 0.75;
                ////diagramhour.AxisY.VisualRange.MaxValue = 250;
                ////diagramhour.AxisY.VisualRange.SideMarginsValue = 10;
                //diagramhour.AxisY.WholeRange.MaxValue = 260;
                //diagramhour.AxisY.WholeRange.MinValue = 150;
                //diagramhour.AxisY.WholeRange.AlwaysShowZeroLevel = true;
                //diagramhour.AxisY.WholeRange.SideMarginsValue = 1;

                ////diagramhour.AxisY.Logarithmic = true;
                ////diagramhour.AxisY.LabelVisibilityMode = AxisLabelVisibilityMode.Default;
                ////diagramhour.AxisY.LogarithmicBase=10;
                //// diagramhour.AxisX.WholeRange.MinValue = 30;
                ////diagramhour.AxisX.Interlaced = true;
                ////diagramhour.AxisX.Label = 10
                ////diagramhour.AxisX.WholeRange.MaxValue= 150;
                ////diagramhour.AxisX.WholeRange.MinValue = 0;
                ////diagramhour.AxisX.Logarithmic = true;
                ////diagramhour.AxisX.LogarithmicBase = 5;

                //ConstantLine constantLineligh = new ConstantLine("250", 250);
                //constantLineligh.Color = Color.AntiqueWhite; //直线颜色
                //constantLineligh.Title.TextColor = Color.Black;   //直线文本字体颜色    
                //constantLineligh.Title.Visible = true;
                //constantLineligh.ShowInLegend = false;
                //diagramhour.AxisY.ConstantLines.Add(constantLineligh);

                //ConstantLine constantLinelow = new ConstantLine((iStandTime * 0.75).ToString(), 150);
                //constantLinelow.Color = Color.AntiqueWhite; //直线颜色
                //constantLinelow.Title.TextColor = Color.Black;   //直线文本字体颜色    
                //constantLinelow.Title.Visible = true;
                //constantLinelow.ShowInLegend = false;
                //diagramhour.AxisY.ConstantLines.Add(constantLinelow);

                //ConstantLine constantLine2 = new ConstantLine("CT:" + iStandTime.ToString(), iStandTime);
                //constantLine2.Color = Color.Green; //直线颜色
                //constantLine2.Title.TextColor = Color.Green;   //直线文本字体颜色    
                //constantLine2.Title.Visible = true;
                //constantLine2.ShowInLegend = false;
                //diagramhour.AxisY.ConstantLines.Add(constantLine2);

                //ConstantLine constantLine3 = new ConstantLine((iStandTime * 1.05).ToString(), iStandTime * 1.05);
                //constantLine3.Color = Color.Red; //直线颜色
                //constantLine3.Title.TextColor = Color.Red;   //直线文本字体颜色    
                //constantLine3.Title.Visible = true;
                //constantLine3.ShowInLegend = false;
                //diagramhour.AxisY.ConstantLines.Add(constantLine3);

                //ConstantLine constantLine4 = new ConstantLine((iStandTime * 0.95).ToString(), iStandTime * 0.95);
                //constantLine4.Color = Color.Yellow; //直线颜色
                //constantLine4.Title.TextColor = Color.Yellow;   //直线文本字体颜色    
                //constantLine4.Title.Visible = true;
                //constantLine4.ShowInLegend = false;
                //diagramhour.AxisY.ConstantLines.Add(constantLine4);
                //diagramhour.AxisY.Title.Text = "";
                //diagramhour.AxisY.Title.Visible = false;
                //diagramhour.AxisY.Title.Alignment = StringAlignment.Center;
                //diagramhour.AxisY.Title.Antialiasing = true;
                //diagramhour.AxisY.Color = Color.Black;
                //diagramhour.AxisX.Color = Color.Black;
                ////diagramhour.AxisX.Title.Text = "icount";
                ////diagramhour.AxisX.Title.Visible = true;
                ////diagramhour.AxisX.Title.Alignment = StringAlignment.Center;
                ////diagramhour.AxisX.Title.Antialiasing = true;
                //横标题设置
                //chartOEEUI.Titles.Clear();
                //ChartTitle titles = new ChartTitle();            //声明标题
                //titles.Text = ProcessName;                            //名称
                //                                                      //titles.TextColor = System.Drawing.Color;   //颜色
                //titles.Indent = 1;                                //设置距离  值越小柱状图就越大
                //titles.Font = new Font("Tahoma", 12, FontStyle.Bold);            //设置字体
                //titles.Dock = ChartTitleDockStyle.Top;           //设置对齐方式
                //titles.Indent = 0;
                //titles.Alignment = StringAlignment.Center;       //居中对齐
                //chartOEEUI.Titles.Add(titles);                 //添加标题  
            }
            catch(Exception ex)
            {
                XtraMessageBox.Show(ex.Message + "oee");
            }

        }

       
    }

    
}
